-
Notifications
You must be signed in to change notification settings - Fork 786
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add PyModule::new_bound
and PyModule::import_bound
#3775
Conversation
CodSpeed Performance ReportMerging #3775 will not alter performanceComparing Summary
|
575e0bf
to
3eb45fb
Compare
PyModule::new
and PyModule::import_bound
PyModule::new_bound
and PyModule::import_bound
3eb45fb
to
2bdc80a
Compare
Ok, I rebased this and it should be in a suitable state now. We still need to resolve #3808, but the diff here is still pretty reviewable without that cleanup. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice to see a lot of as_borrowed
and as_gil_ref
disappear!
wrap_pyfunction
is a bit of a bound-ref-bound circle for now, but that's ok. I noticed a type error in a place or two, that the compiler/ci shout also pickup, and a few style nits.
A bit unrelated, but while reviewing I noticed that this example also needs updating. And since compile_fail
examples tend to fail for the wrong reason as time passes, I might as well mention it, before I forget again 😄 .
Lines 609 to 636 in 2bdc80a
/// ```compile_fail | |
/// # use pyo3::prelude::*; | |
/// # use pyo3::types::PyDict; | |
/// # | |
/// #[pyclass] | |
/// struct Foo<'py> { | |
/// inner: &'py PyDict, | |
/// } | |
/// | |
/// impl Foo { | |
/// fn new() -> Foo { | |
/// let foo = Python::with_gil(|py| { | |
/// // `py` will only last for this scope. | |
/// | |
/// // `&PyDict` derives its lifetime from `py` and | |
/// // so won't be able to outlive this closure. | |
/// let dict: &PyDict = PyDict::new(py); | |
/// | |
/// // because `Foo` contains `dict` its lifetime | |
/// // is now also tied to `py`. | |
/// Foo { inner: dict } | |
/// }); | |
/// // Foo is no longer valid. | |
/// // Returning it from this function is a 💥 compiler error 💥 | |
/// foo | |
/// } | |
/// } | |
/// ``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can change some more &PyModule
to &Bound<'_, PyModule>
, but maybe these should all wait until we update the wrap_pyfunction!
macro to support Bound
.
@@ -78,9 +78,9 @@ fn parent_module(py: Python<'_>, m: &PyModule) -> PyResult<()> { | |||
} | |||
|
|||
fn register_child_module(py: Python<'_>, parent_module: &PyModule) -> PyResult<()> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we do:
fn register_child_module(py: Python<'_>, parent_module: &PyModule) -> PyResult<()> { | |
fn register_child_module(py: Python<'_>, parent_module: &Bound<'_, PyModule>) -> PyResult<()> { |
parent_module.add_submodule(child_module)?; | ||
let child_module = PyModule::new_bound(py, "child_module")?; | ||
child_module.add_function(&wrap_pyfunction!(func, child_module.as_gil_ref())?.as_borrowed())?; | ||
parent_module.add_submodule(child_module.as_gil_ref())?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
parent_module.add_submodule(child_module.as_gil_ref())?; | |
parent_module.add_submodule(child_module)?; |
@@ -286,10 +336,10 @@ impl PyModule { | |||
/// | |||
/// #[pymodule] | |||
/// fn my_module(py: Python<'_>, module: &PyModule) -> PyResult<()> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// fn my_module(py: Python<'_>, module: &PyModule) -> PyResult<()> { | |
/// fn my_module(py: Python<'_>, module: &Bound<'_, PyModule>) -> PyResult<()> { |
/// submodule.add("super_useful_constant", "important")?; | ||
/// | ||
/// module.add_submodule(submodule)?; | ||
/// module.add_submodule(submodule.as_gil_ref())?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// module.add_submodule(submodule.as_gil_ref())?; | |
/// module.add_submodule(submodule)?; |
@@ -480,10 +530,10 @@ pub trait PyModuleMethods<'py> { | |||
/// | |||
/// #[pymodule] | |||
/// fn my_module(py: Python<'_>, module: &PyModule) -> PyResult<()> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// fn my_module(py: Python<'_>, module: &PyModule) -> PyResult<()> { | |
/// fn my_module(py: Python<'_>, module: &Bound<'_, PyModule>) -> PyResult<()> { |
/// submodule.add("super_useful_constant", "important")?; | ||
/// | ||
/// module.add_submodule(submodule)?; | ||
/// module.add_submodule(submodule.as_gil_ref())?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// module.add_submodule(submodule.as_gil_ref())?; | |
/// module.add_submodule(submodule)?; |
@@ -258,12 +258,12 @@ fn superfunction() -> String { | |||
#[pymodule] | |||
fn supermodule(py: Python<'_>, module: &PyModule) -> PyResult<()> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fn supermodule(py: Python<'_>, module: &PyModule) -> PyResult<()> { | |
fn supermodule(py: Python<'_>, module: &Bound<'_, PyModule>) -> PyResult<()> { |
module.add_submodule(module_to_add)?; | ||
let module_to_add = PyModule::new_bound(py, "submodule")?; | ||
submodule(&module_to_add)?; | ||
module.add_submodule(module_to_add.as_gil_ref())?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
module.add_submodule(module_to_add.as_gil_ref())?; | |
module.add_submodule(module_to_add)?; |
module.add_submodule(module_to_add.as_gil_ref())?; | ||
let module_to_add = PyModule::new_bound(py, "submodule_with_init_fn")?; | ||
submodule_with_init_fn(py, module_to_add.as_gil_ref())?; | ||
module.add_submodule(module_to_add.as_gil_ref())?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
module.add_submodule(module_to_add.as_gil_ref())?; | |
module.add_submodule(module_to_add)?; |
Indeed, |
@LilyFoote regarding the suggestions above, the blocker is the support for |
Ooof can't keep up with the merge conflicts! I'll try to fixup this again this evening and hopefully see it across the line. |
Part of #3684
This is a WIP migration of
PyModule::new
andPyModule::import
to the new APIs.I decided to pause on this one for a bit and leave in draft, because:
Python::import
toPython::import_bound
at the same time, but this got too much so I might try to do that one first.#[pymodule]
taking&Bound<'_, PyModule>
(probably in a similar way to the draft support&Bound<PyType>
forclassmethod
#3744) before continuing here, because a lot of the examples / docs get in a really bad interim state here otherwise.